package com.css.fxfzcpxx.modules.hddcMap.repository.impl;

import com.css.fxfzcpxx.constants.YNEnum;
import com.css.fxfzcpxx.modules.hddcMap.entity.DrawingEntity;
import com.css.fxfzcpxx.modules.hddcMap.repository.HddcMapRepository;
import com.css.fxfzcpxx.modules.hddcMap.vo.UpdateAttachVO;
import com.css.fxfzcpxx.util.PlatformDateUtils;
import com.css.fxfzcpxx.util.PlatformSessionUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @description
 */
@Repository
public class HddcMapRepositoryImpl implements HddcMapRepository {

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Resource
    private JdbcTemplate jdbcTemplate;

    //cpxx_thematic_map
    @Override
    public void save(DrawingEntity entity) {
        String sql="insert into cpxx_thematic_map " +
                "(id,means_name,release_column,release_status,release_date,province,city,county,picture_file,picture_describe," +
                "keyword,del_flag,create_user,create_time,plot_time,drafting_unit,publishing_unit,number,flag,magnitude,type) " +
                "values (:id,:title,:releaseColumn,:releaseStatus,:drawDate,:province,:city,:county,:attachUrl,:description," +
                ":keyWords,:delFlag,:createUser,:createTime,:drawDate,:drawingUnit,:drawingUnit,:number,:flag,:mm,:type)";
        BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(entity);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void updateAttachVO(UpdateAttachVO updateAttachVO) {
        String sql = "update cpxx_thematic_map set picture_file=:attachUrl,update_user=:updateUser,update_time='"+PlatformDateUtils.getCurrentTimestamp()+"'  " +
                "where id=:id";
        SqlParameterSource source = new BeanPropertySqlParameterSource(updateAttachVO);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        namedParameterJdbcTemplate.update(sql, source, keyHolder);
    }

    @Override
    public void delete(List<String> ids) {
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        String userId = PlatformSessionUtils.getUserId();
        String sql = "update cpxx_thematic_map set del_flag = '" + YNEnum.Y + "' , update_time = '" + timestamp + "' , update_user ='"+userId+"' " + " where id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }
}
